local love = require 'love'
local fontHandler = require('assets-handler.font-handler')

local setColor = love.graphics.setColor
local circle = love.graphics.circle
local printg = love.graphics.print
local setLineWidth = love.graphics.setLineWidth
local drawRectangle = love.graphics.rectangle

local width = Width
local height = Height
local padding = Padding

-- 出牌圆形按钮
local playButton = {
    radius = 0,
    y = 0,
    x = 0,
    rectX = 0,
    rectY = 0,
    rectWidth = 0,
    rectHeight = 0,
    -- 出牌圆形按钮文字
    circleText = "出牌",
    circleTextWidth = 0,
    circleTextHeight = 0,
    rectTextX = 0,
    rectTextY = 0,
    -- 开始按钮使用绿色, 出牌按钮使用红色, 结束按钮使用蓝色
    normalColor = {{0.298, 0.686, 0.314, 1.0}, -- #4CAF50(草绿色)
                   {0.957, 0.263, 0.212, 1.0}, -- #e53935(深红色)
                   {0.129, 0.588, 0.953, 1.0}}, -- #2196F3(天空蓝)
    hoveredColor = {{0.4, 0.733, 0.416, 1.0}, -- #66BB6A(淡草绿)
                    {0.937, 0.325, 0.314, 1.0}, -- #ef5350(亮红色)
                    {0.259, 0.647, 0.961, 1.0}}, -- #42A5F5(较浅的蓝色)
    pressedColor = {{0.219, 0.556, 0.235, 1.0}, -- #388E3C(深绿色)
                    {0.827, 0.184, 0.184, 1.0}, -- #D32F2F(偏亮的深红色)
                    {0.1, 0.463, 0.839, 1.0}}, -- #1976D2(较深的蓝色)
    currentColor = {},
    colorType = 1,
    circleRadius = 0,
    fontSize = 24,
    startFontSize = 48,
    textX = 0,
    textY = 0,
    isHovered = false,
    isPressed = false
}

local function init()
    playButton.circleRadius = height / 20
    playButton.radius = playButton.circleRadius
    playButton.y = height - playButton.circleRadius - padding * 3
    playButton.x = width / 2
    playButton.rectWidth = playButton.radius * 3.4
    playButton.rectHeight = playButton.radius * 1.7
    playButton.rectX = (width - playButton.rectWidth) / 2
    playButton.rectY = (height - playButton.rectHeight) * (1 - 0.618)

    playButton.currentColor = playButton.normalColor[playButton.colorType]
    -- 初始化字体
    fontHandler.setFont(playButton.fontSize)
    playButton.circleTextWidth = fontHandler.getFontWidth(playButton.circleText)
    playButton.circleTextHeight = fontHandler.getFontHeight()
    playButton.textX = playButton.x - playButton.circleTextWidth / 2 -- X坐标居中
    playButton.textY = playButton.y - playButton.circleTextHeight / 2.4 -- Y坐标居中

    fontHandler.setChineseFont(playButton.startFontSize)
    playButton.circleTextWidth = fontHandler.getFontWidth(playButton.circleText)
    playButton.circleTextHeight = fontHandler.getFontHeight()
    playButton.rectTextX = playButton.rectX + playButton.rectWidth / 2 - playButton.circleTextWidth / 1.9 -- X坐标居中
    playButton.rectTextY = playButton.rectY + playButton.rectHeight / 2 - playButton.circleTextHeight / 2.1 -- Y坐标居中
end

-- 绘制圆形按钮
local function drawPlayButton(playStep, match)
    if playStep == 1 then
        playButton.circleText = "开始"
        playButton.colorType  = 1
    elseif playStep == 2 then
        playButton.circleText = "发牌"
        playButton.colorType = 2
    elseif playStep == 3 or playStep == 4 then
        playButton.circleText = "出牌"
        playButton.colorType  = 2
    elseif playStep == 6 then
        playButton.circleText = "重来"
        playButton.colorType  = 3
    end

    -- 更新按钮颜色
    if playButton.isPressed then
        playButton.currentColor = playButton.pressedColor[playButton.colorType]
    elseif playButton.isHovered then
        playButton.currentColor = playButton.hoveredColor[playButton.colorType]
    else
        playButton.currentColor = playButton.normalColor[playButton.colorType]
    end


    if playButton.colorType == 1 or playButton.colorType == 3 then
        -- 绘制按钮
        setColor(playButton.currentColor)
        drawRectangle("fill", playButton.rectX, playButton.rectY, playButton.rectWidth, playButton.rectHeight, 12, 12)

        -- 绘制按钮边框
        setColor(0.2, 0.2, 0.2, 1)
        setLineWidth(1)
        drawRectangle("line", playButton.rectX, playButton.rectY, playButton.rectWidth, playButton.rectHeight, 12, 12)

        -- 绘制文本
        fontHandler.setChineseFont(playButton.startFontSize)
        setColor(1, 1, 1)
        printg(playButton.circleText, playButton.rectTextX, playButton.rectTextY)

    elseif playButton.colorType == 2 then
        -- 绘制圆形
        setColor(playButton.currentColor)
        circle("fill", playButton.x, playButton.y, playButton.radius, 50)

        -- 绘制按钮边框
        setColor(0.2, 0.2, 0.2, 1)
        setLineWidth(1)
        circle("line", playButton.x, playButton.y, playButton.radius, 50)

        -- 绘制文本
        fontHandler.setFont(playButton.fontSize)
        setColor(1, 1, 1)
        printg(playButton.circleText, playButton.textX, playButton.textY)
    end
end

local function inArea(x, y)
    if playButton.colorType == 1 or playButton.colorType == 3 then
        return x >= playButton.rectX and x <= playButton.rectX + playButton.rectWidth and y >= playButton.rectY and y <= playButton.rectY + playButton.rectHeight
    elseif playButton.colorType == 2 then
        return x >= playButton.x - playButton.radius and x <= playButton.x + playButton.radius and y >= playButton.y - playButton.radius and y <= playButton.y + playButton.radius
    end
end

local function isHovered(boolean)
    playButton.isHovered = boolean
end

local function isPressed()
    playButton.isPressed = true
end

local function isReleased()
    playButton.isPressed = false
end

return{
    init = init,
    drawPlayButton = drawPlayButton,
    inArea = inArea,
    isHovered = isHovered,
    isPressed = isPressed,
    isReleased = isReleased
}